<?php
/**
 * Description: 预约模考
 * User: devforma
 * Date: 15/9/22
 * Time: 11:49
 */

class BookMock implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb, $uddb;

        if (validateUser($queryArgs) !== 1 || empty($queryArgs['mock_id']) || !filter_var($queryArgs['mock_id'], FILTER_VALIDATE_INT))
            return genOutput(1000);

        if (empty($queryArgs['phone'])) {
            $phone = $uddb->getVar('SELECT `phone` FROM `user_auth_info` WHERE `ID`=:uid', array(':uid' => $queryArgs['user_id']));
            if (empty($phone))
                return genOutput(1000);
        } else {
            $phone = $queryArgs['phone'];
        }

        $mockInfo = $cmsdb->getVar('SELECT `paper_customed_detail` FROM `data_test_paper` WHERE `id`=:mid',
            array(':mid' => $queryArgs['mock_id']));
        if (empty($mockInfo))
            return genOutput(1000);

        $mockInfo = unserialize($mockInfo);
        if (empty($mockInfo['startTime']))
            return genOutput(1000);

        $uddb->insert('book_mock_users', array(
            'user_id' => $queryArgs['user_id'],
            'mock_id' => $queryArgs['mock_id'],
            'mock_start_time' => $mockInfo['startTime'],
            'book_time' => date('Y-m-d H:i:s'),
            'phone' => $phone,
            'terminal_type' => $queryArgs['terminal_type'],
            'msg_sended' => 0
        ));

        return genOutput(1);
    }
}